//
//  InputView.m
//  UESTCPioneer
//
//  Created by Apple on 14-7-19.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "InputView.h"
#define kEachInputFieldHeight 40
@interface InputView(){
    UIView *preSeperator,*preLabel;
    
}
@end

@implementation InputView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



-(id)initWithLabelTextArray:(NSArray*)array  viewFrame:(CGRect)viewFrame{
    self = [super init];
    if (self){
        
        CGFloat width = viewFrame.size.width;
//        CGFloat height = viewFrame.size.height;
        //背景
        UIView *backgroundImageView = [[UIView alloc]initWithFrame:viewFrame];
        [backgroundImageView.layer setCornerRadius:5];
        [backgroundImageView.layer setBorderWidth:1];
        [backgroundImageView.layer setBorderColor:[UIColor colorWithWhite:146.0/255.0 alpha:0.5].CGColor];
        
        [self addSubview:backgroundImageView];
        NSMutableArray *tempFieldArray = [[NSMutableArray alloc]init];
        
        for (NSInteger i=0;i<array.count;i++){
            //分割线
            UIImageView *seperator = [[UIImageView alloc]init];
            [seperator setImage:[UIImage imageNamed:@"sepline"]];
            [backgroundImageView addSubview:seperator];
            [seperator setContentMode:UIViewContentModeRedraw];
            [seperator setTranslatesAutoresizingMaskIntoConstraints:NO];
            [backgroundImageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[seperator]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(seperator)]];
            [backgroundImageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[seperator(==0.5)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(seperator)]];
            //标签
            
            CGFloat labelY = kEachInputFieldHeight * i;
            UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(10, labelY, 100, kEachInputFieldHeight)];
            [label setText:array[i]];
            [backgroundImageView addSubview:label];
            //输入框
//warning  为了省时间暂时这么hack满足注册页面
            id textField;
            
            CGFloat textFieldX = CGRectGetMaxX(label.frame) + kEachInputFieldHeight;
            if (i>=3&&i<=6){
                textField = [[UILabel alloc]initWithFrame:CGRectMake(textFieldX, labelY, width - textFieldX - 10, kEachInputFieldHeight)];
            } else {
                textField = [[UITextField alloc]initWithFrame:CGRectMake(textFieldX, labelY, width - textFieldX - 10, kEachInputFieldHeight)];
            }
            [backgroundImageView addSubview:textField];
            
//            [textField setTranslatesAutoresizingMaskIntoConstraints:NO];
            [tempFieldArray addObject:textField];
            
            if (i==0){
                [backgroundImageView addConstraint:[NSLayoutConstraint constraintWithItem:seperator attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeTop multiplier:1 constant:kEachInputFieldHeight]];
                
                [backgroundImageView addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
            } else {
                
                [backgroundImageView addConstraint:[NSLayoutConstraint constraintWithItem:seperator attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:preSeperator attribute:NSLayoutAttributeBottom multiplier:1 constant:kEachInputFieldHeight]];
                [backgroundImageView addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:preLabel attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
            }
            preSeperator=seperator;
            preLabel=label;
            
            //            [backgroundImageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[preLabel][textField]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(preLabel,textField)]];
            [backgroundImageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-110-[textField(==180)]|" options:0 metrics:Nil views:NSDictionaryOfVariableBindings(textField)]];
            [backgroundImageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textField(==preLabel)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(preLabel,textField)]];
            [backgroundImageView addConstraint:[NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:preLabel attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
            
        }
        [backgroundImageView addConstraint:[NSLayoutConstraint constraintWithItem:preLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
        [preSeperator removeFromSuperview];
        _TextFieldArray=tempFieldArray;
    }
    return self;
}

@end
